package com.grant.code.controller;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import com.grant.code.Main;
import com.grant.code.exception.AuthException;
import com.grant.code.pojo.User;
import com.grant.code.service.UserService;
import com.grant.code.service.impl.UserServiceImpl;
import com.grant.code.utils.DialogUtils;
import com.grant.code.utils.SpringContextUtils;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.io.IOException;
import java.net.URL;
import java.util.Objects;
import java.util.ResourceBundle;

import static com.grant.code.utils.DialogUtils.showErrorDialog;
import static com.grant.code.utils.DialogUtils.showSuccessDialog;


@FXMLController
public class LoginController implements Initializable {
    @FXML
    private TextField txtUsername;
    @FXML private PasswordField txtPassword;

    private UserService userService;
    @Override
   public void initialize(URL location, ResourceBundle resources) {
        ApplicationContext context = Main.getContext();
        userService = context.getBean(UserService.class);
        System.out.println(context);
   }



    @FXML
    private void onLoginClick() {
        String username = txtUsername.getText().trim();
        String password = txtPassword.getText().trim();

        try {
            User user = userService.login(new User(username, password));
            DialogUtils.showSuccessDialog("登录成功", () -> showMainView(user));
        } catch (AuthException e) {
            DialogUtils.showErrorDialog("登录失败", "请检查用户名或密码是否输入正确");
        }
    }
    // 显示主界面
    private void showMainView(User user) {
        try {
            Stage currentStage = (Stage) txtUsername.getScene().getWindow();
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/home.fxml"));
            Parent root = loader.load();

            // 传递用户数据到主界面控制器
            MainController controller = loader.getController();
            controller.initData(user);

            Stage mainStage = new Stage();
            mainStage.setScene(new Scene(root));
            mainStage.setTitle("电子图片管理系统 - 主界面");
            mainStage.setUserData(controller); // 将MainController实例设置到userData
            mainStage.setOnCloseRequest(e -> Main.shutdown());

            currentStage.close(); // 关闭登录窗口
            mainStage.show();

            // 更新Main类的primaryStage为新的主Stage
            Main.setPrimaryStage(mainStage);
        } catch (IOException e) {
            e.printStackTrace();
            DialogUtils.showErrorDialog("界面加载失败");
        }
    }

    @FXML
    private void navigateToRegister() {
        try {
            Stage currentStage = (Stage) txtUsername.getScene().getWindow();
            
            Parent root = FXMLLoader.load(getClass().getResource("/fxml/register.fxml"));
            Scene newScene = new Scene(root);

            currentStage.setScene(newScene);
        } catch (IOException e) {
            e.printStackTrace();
            DialogUtils.showErrorDialog("无法打开注册页面");
        }
    }


}